package com.xuecheng.content.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import com.xuecheng.api.content.model.dto.CourseTeacherDTO;
import com.xuecheng.api.content.model.dto.TeachplanDTO;
import com.xuecheng.common.domain.code.CommonErrorCode;

import com.xuecheng.common.enums.common.CommonEnum;
import com.xuecheng.common.enums.common.ResourceTypeEnum;
import com.xuecheng.common.enums.content.CourseAuditEnum;
import com.xuecheng.common.exception.ExceptionCast;
import com.xuecheng.content.common.constant.ContentErrorCode;
import com.xuecheng.content.convert.CourseteacherConvert;
import com.xuecheng.content.convert.TeachplanConvert;
import com.xuecheng.content.entity.CourseBase;
import com.xuecheng.content.entity.CourseTeacher;
import com.xuecheng.content.entity.Teachplan;
import com.xuecheng.content.mapper.CourseTeacherMapper;
import com.xuecheng.content.service.CourseBaseService;
import com.xuecheng.content.service.CourseTeacherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * <p>
 * 课程-教师关系表 服务实现类
 * </p>
 *
 * @author itcast
 */
@Slf4j
@Service
public class CourseTeacherServiceImpl extends ServiceImpl<CourseTeacherMapper, CourseTeacher> implements CourseTeacherService {

    @Autowired
    private CourseBaseService courseBaseService;


    //根据ID查老师的数据
    @Override
    public List<CourseTeacherDTO> queryCourseTeacherbyCourseId(Long courseBaseId, Long companyId) {
        //1.判断关键数据
        if (ObjectUtils.isEmpty(courseBaseId)||ObjectUtils.isEmpty(companyId)){
            ExceptionCast.cast(CommonErrorCode.E_100101);
        }
        //2.判断业务数据
        LambdaQueryWrapper<CourseBase> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CourseBase ::getId,courseBaseId);
        wrapper.eq(CourseBase ::getCompanyId,companyId);
        CourseBase courseBase = courseBaseService.getOne(wrapper);
        if (ObjectUtils.isEmpty(courseBase)){
            ExceptionCast.cast(CommonErrorCode.E_100104);
        }
        LambdaQueryWrapper<CourseTeacher> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CourseTeacher::getCourseId,courseBaseId);
        //CourseTeacher courseTeacher = this.getOne(lambdaQueryWrapper);
        List<CourseTeacher> list = this.list(lambdaQueryWrapper);
        /*if (ObjectUtils.isEmpty(list)){
            ExceptionCast.cast(CommonErrorCode.E_100104);
        }*/
        List<CourseTeacherDTO> courseTeacherDTOS = CourseteacherConvert.INSTANCE.entitys2dtos(list);
        return courseTeacherDTOS;
    }


    //新增和修改教师信息功能（总1）
    @Override
    @Transactional
    public CourseTeacherDTO creatCourseTeacher(CourseTeacherDTO courseTeacherDTO) {
        CourseTeacherDTO resultDTO= null;
        if (ObjectUtils.isEmpty(courseTeacherDTO.getId())){
            resultDTO=createCourseTeacher1(courseTeacherDTO);
        }else {
            resultDTO=modifyCourseTeacher1(courseTeacherDTO);
        }
        return resultDTO;
    }

    //删除教师信息功能
    /*  1.传入数据是否为空
        2.根据coursebaseid 和companyid ，课程是否存在
        3.判断老师是否存在
        3.判断课程状态是否被删除
        4.进行删除，返回前端
     */
    @Override
    @Transactional
    public void removeCourseTeacher(Long courseBaseId, Long courseTeacherId) {
        if (ObjectUtils.isEmpty(courseBaseId)||
            ObjectUtils.isEmpty(courseTeacherId)){
            ExceptionCast.cast(ContentErrorCode.E_120402);
        }
        LambdaQueryWrapper<CourseBase> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CourseBase::getId,courseBaseId);
        CourseBase courseBase = courseBaseService.getById(lambdaQueryWrapper);
        if (ObjectUtils.isEmpty(courseBase)){
            ExceptionCast.cast(ContentErrorCode.E_120013);
        }
        CourseTeacher courseTeacher = this.getById(courseTeacherId);
        if (ObjectUtils.isEmpty(courseTeacher)){
            ExceptionCast.cast(ContentErrorCode.E_120013);
        }
        Integer status = courseBase.getStatus();
        if (CommonEnum.DELETE_FLAG.equals(status)){
            ExceptionCast.cast(ContentErrorCode.E_120021);
        }
        this.removeById(courseTeacherId);
    }


    //创建课程教师信息功能（1-1）
    private CourseTeacherDTO createCourseTeacher1(CourseTeacherDTO courseTeacherDTO) {
        CourseTeacherDTO teacherDTO = verifyTeachplanMsg(courseTeacherDTO);
        CourseTeacher courseTeacher = CourseteacherConvert.INSTANCE.dto2entity(teacherDTO);
        boolean result = this.save(courseTeacher);
        if (!result){
            ExceptionCast.cast(ContentErrorCode.E_120407);
        }
        CourseTeacher courseTeacher1 = this.getById(courseTeacher.getId());
        CourseTeacherDTO courseTeacherDTO1 = CourseteacherConvert.INSTANCE.entity2dto(courseTeacher1);
        return courseTeacherDTO1;
    }
    //修改课程教师信息功能（1-2）
    private CourseTeacherDTO modifyCourseTeacher1(CourseTeacherDTO courseTeacherDTO) {
        CourseTeacherDTO teacherDTO = verifyTeachplanMsg(courseTeacherDTO);
        LambdaUpdateWrapper<CourseTeacher> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(CourseTeacher::getCourseId,courseTeacherDTO.getCourseId());
        updateWrapper.set(CourseTeacher::getPhotograph,courseTeacherDTO.getPhotograph());
        updateWrapper.set(CourseTeacher::getPosition,courseTeacherDTO.getPosition());
        updateWrapper.set(CourseTeacher::getTeacherName,courseTeacherDTO.getTeacherName());
        updateWrapper.set(CourseTeacher::getIntroduction,courseTeacherDTO.getIntroduction());
        //4.判断修改后的结果
        boolean result = this.update(updateWrapper);
        if (!result) {
            ExceptionCast.cast(ContentErrorCode.E_120407);
        }
        CourseTeacher courseTeacher = this.getById(courseTeacherDTO.getId());
        CourseTeacherDTO resultDTO = CourseteacherConvert.INSTANCE.entity2dto(courseTeacher);
        return resultDTO;

    }

    //判断数据
    private CourseTeacherDTO verifyTeachplanMsg(CourseTeacherDTO courseTeacherDTO) {
        //1.判断关键数据
        if (ObjectUtils.isEmpty(courseTeacherDTO.getPhotograph())||
            ObjectUtils.isEmpty(courseTeacherDTO.getPosition())||
            ObjectUtils.isEmpty(courseTeacherDTO.getTeacherName())){
            ExceptionCast.cast(CommonErrorCode.E_100101);
        }
        //2.判断业务数据
        CourseBase courseBase = courseBaseService.getById(courseTeacherDTO.getCourseId());
        if (ObjectUtils.isEmpty(courseBase)){
            ExceptionCast.cast(CommonErrorCode.E_100101);
        }
        String auditStatus = courseBase.getAuditStatus();
        if (CourseAuditEnum.AUDIT_PASTED_STATUS.getCode().equals(auditStatus)||
                CourseAuditEnum.AUDIT_COMMIT_STATUS.getCode().equals(auditStatus)||
                CourseAuditEnum.AUDIT_PUBLISHED_STATUS.getCode().equals(auditStatus)
        ) {
            ExceptionCast.cast(ContentErrorCode.E_120015);
        }
        return courseTeacherDTO;

    }

    }


